package pl.edu.pk.adcampaignfacebook.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import pl.edu.pk.adcampaignfacebook.domain.Feed;
import pl.edu.pk.adcampaignfacebook.dto.UserService;
import pl.edu.pk.adcampaignfacebook.integration.FeedMap;
import pl.edu.pk.adcampaignfacebook.integration.IntegrationPhoto;
import pl.edu.pk.adcampaignfacebook.interfaces.integration.IntegrationAPI;

@Controller
public class ManageController
{
	private static Logger logger = Logger.getLogger(ManageController.class);
	private Properties integrationProperties = new Properties();
	
	@RequestMapping(value = "/manage/index")
	public ModelAndView index()
	{
		ModelAndView mav = new ModelAndView("/manage/index");
		logger.debug("displays manage/index");
		return mav;
	}
	
	@RequestMapping(value = "/manage/readfeed")
	public ModelAndView list()
	{
		ModelAndView mav = new ModelAndView("/manage/readfeed");
		return mav;
	}
	
	@RequestMapping(value = "/manage/integrationfeed", method = RequestMethod.GET)
	public ModelAndView getIntegrationPhoto()
	{
		logger.info("xxxxx integartion photo");
		ModelAndView mav = new ModelAndView("/manage/integrationfeed");
		JSONArray array = new JSONArray();
		
		List<IntegrationPhoto> list =  FeedMap.getFeedList("admin");
		for(IntegrationPhoto integrationPhoto : list) {
			JSONObject jsonObject = new JSONObject();
			try {
				jsonObject.put("url", integrationPhoto.getUrl());
				jsonObject.put("title", integrationPhoto.getTitle());
				jsonObject.put("description", integrationPhoto.getDescription());
			} catch (JSONException e) {
				e.printStackTrace();
			}
			array.put(jsonObject);
		}
		mav.addObject("photos", list);
		mav.addObject("jsonPhotos", array.toString());
		return mav;
	}
	
	
	@RequestMapping(value = "/manage/postfeed", method = RequestMethod.GET)
	public ModelAndView add()
	{
		logger.info("xxxxx add get");
		ModelAndView mav = new ModelAndView("/manage/postfeed");
		mav.addObject("feed", new Feed());
		return mav;
	}
	
	@RequestMapping(value = "/manage/postfeed", method = RequestMethod.POST)
	public ModelAndView add(@Valid Feed feed, BindingResult result)
	{
//		InputStream in = this.getClass().getResourceAsStream("adfb/src/main/webapp/integration.properties");
//		assert in == null;
//		try {
//			integrationProperties.load(in);
//		} catch (IOException e) {
//			logger.error("Problem with loading properties: ", e);
//			e.printStackTrace(); 
//		}
		
		logger.info("xxxxx add post");
		if(feed.getSendToOtherServices() == true) {
			logger.info("to all services");
			
			UserService userService = new UserService();
			userService.setId(1L);
			userService.setIdOnService("drake");
			userService.setSecretToken("password");
			userService.setToken("http://85.28.131.213:8080/photointegrator/api");
			
			IntegrationAPI integrationAPI = new IntegrationAPI();
			integrationAPI.setContext(userService);
			
			logger.info("token : " + integrationAPI.getToken());
			
			integrationAPI.addPhoto(feed.getFeedPicture(), feed.getFeedPost(), feed.getFeedPost());
			
			
		} 
		ModelAndView mav = new ModelAndView("/manage/index");
		return mav;
	}
}
